package com.liqi.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liqi.device.domain.TWorkingCalendar;
import com.liqi.device.mapper.TWorkingCalendarMapper;
import com.liqi.device.service.TWorkingCalendarService;
import com.liqi.step.domain.TRelServiceStep;
import com.liqi.step.mapper.TRelServiceStepMapper;
import com.liqi.step.service.TRelServiceStepService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 节假日调班日表服务层
 * @author zhengkai.blog.csdn.net
 * @date 2023-03-22
 */
@Service
public class TWorkingCalendarServiceImpl extends ServiceImpl<TWorkingCalendarMapper, TWorkingCalendar> implements TWorkingCalendarService {

    @Autowired
    private TWorkingCalendarMapper workingCalendarMapper;
    @Override
    public List<TWorkingCalendar> resolveCalendarInfo(String year, String month) {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://opendata.baidu.com/api.php?query=" + year + "年" + month + "月&resource_id=39043&format=json&tn=wisetpl";
        JSONObject result = JSONObject.parseObject(restTemplate.getForObject(url, String.class));
        System.out.println("result = " + result);
        List<TWorkingCalendar> calendarInfos = new ArrayList<>();
        try {
            if ("0".equalsIgnoreCase(result.getString("status"))) {
                JSONArray data = result.getJSONArray("data");
                if (!CollectionUtils.isEmpty(data)) {
                    JSONObject item = data.getJSONObject(0);
                    if (item == null || item.get("almanac") == null) {
                        return calendarInfos;
                    }
                    JSONArray jsonArray = item.getJSONArray("almanac");
                    if (!CollectionUtils.isEmpty(jsonArray)) {
                        for (int i = 0; i < jsonArray.size(); i++) {
                            JSONObject h = jsonArray.getJSONObject(i);
                            String calendarDate = h.getString("year") + "-" + (h.getInteger("month") > 9 ? h.getString("month") : "0" + h.getString("month")) + "-" + (h.getInteger("day") > 9 ? h.getString("day") : "0" + h.getString("day"));
                            String calendarType = h.getString("status");
                            String calendarDetail = h.getString("term");
                            String cnDay = h.getString("cnDay");
                            // 生成对象
                            TWorkingCalendar TWorkingCalendar = com.liqi.device.domain.TWorkingCalendar.builder().calendarDate(calendarDate).cnDay(cnDay).calendarType(StringUtils.isBlank(calendarType) ? null : Integer.valueOf(calendarType))
                                    .build();
                            calendarInfos.add(TWorkingCalendar);
                        }
                    }
                }
            }
        } catch (Exception e) {
            log.error("resolveCalendarInfo is error");
            throw new RuntimeException(e);
        }

        return calendarInfos;
    }

}